package com.forsir.android.mhfinance;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.forsir.android.mhfinance.helper.DbAdapter;
import com.forsir.android.mhfinance.storeObjects.Payment;
import com.forsir.android.mhfinance.storeObjects.PaymentList;
import com.forsir.android.mhfinance.storeObjects.Repeating;

public class ShowPayment extends Activity {
	public static final String REQUEST_PAYMENT_ID = "payment_id";
	public static final String REQUEST_POSITION = "position";

	private Payment payment = null;
	private int position;

	private SharedPreferences mPreferenceSettings;
	private DbAdapter mDbHelper;
	private Resources resource;
	private TextView accountValue;
	private TextView accountText;
	private TextView categoryValue;
	private TextView categoryText;
	private TextView dateValue;
	private TextView typeValue;
	private TextView typeText;
	private TextView amountText;
	private TextView remainderText;
	private TextView remainderDelimiter;
	private TextView descriptionValue;
	private TextView descriptionText;
	private TextView transferAccountValue;
	private TextView transferText;
	private TextView repeatingTypeValue;
	private TextView repeatingTypeText;
	private TextView repeatingTypeDate;
	private Button nextButton;
	private Button previousButton;
	private Button closeButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_payment);

		mDbHelper = new DbAdapter(this, this);
		mDbHelper.open();

		resource = getResources();

		setElements();

		mPreferenceSettings = PreferenceManager.getDefaultSharedPreferences(this);

		final Bundle extras = getIntent().getExtras();
		if (extras != null) {
			position = extras.getInt(REQUEST_POSITION, -1);
			if (position >= 0) {
				payment = PaymentList.getPayment(position);
			}

			if (payment == null) {
				payment.setId(extras.getLong(REQUEST_PAYMENT_ID, -1));
				payment.loadFromDb(mDbHelper);
			}
		}

		if (savedInstanceState != null) {
			payment.fillFromBundle(savedInstanceState);
			payment.getRepeating().fillFromBundle(savedInstanceState);
		}

		if (payment != null) {
			payment.getRepeating().computeOccurrences(mDbHelper);
		}

		fillForm();

		// set listeners
		closeButton.setOnClickListener(cancelListener);
		previousButton.setOnClickListener(previousClickListener);
		nextButton.setOnClickListener(nextClickListener);
	}

	private void setElements() {
		accountValue = (TextView) findViewById(R.id.payment_account);
		accountText = (TextView) findViewById(R.id.payment_account_text);
		categoryValue = (TextView) findViewById(R.id.payment_category);
		categoryText = (TextView) findViewById(R.id.payment_category_text);
		dateValue = (TextView) findViewById(R.id.payment_date);
		typeValue = (TextView) findViewById(R.id.payment_type);
		typeText = (TextView) findViewById(R.id.payment_type_text);
		amountText = (TextView) findViewById(R.id.payment_amount);
		remainderText = (TextView) findViewById(R.id.payment_remainder);
		remainderDelimiter = (TextView) findViewById(R.id.payment_remainder_delimiter);
		descriptionValue = (TextView) findViewById(R.id.payment_description);
		descriptionText = (TextView) findViewById(R.id.payment_description_text);
		transferAccountValue = (TextView) findViewById(R.id.payment_transfer_account);
		transferText = (TextView) findViewById(R.id.payment_transfer_account_text);
		repeatingTypeValue = (TextView) findViewById(R.id.payment_repeating_type);
		repeatingTypeText = (TextView) findViewById(R.id.payment_repeating_type_text);
		repeatingTypeDate = (TextView) findViewById(R.id.payment_repeating_date);

		nextButton = (Button) findViewById(R.id.payment_next);
		previousButton = (Button) findViewById(R.id.payment_previous);
		closeButton = (Button) findViewById(R.id.payment_close);
	}

	private void fillForm() {
		accountValue.setText(payment.getAccount().getName());
		dateValue.setText(payment.getDateString(getResources()) + ", " + payment.getTimeString(getResources()));
		final String currencyBefore = mPreferenceSettings.getString(getString(R.string.key_currency_before), getString(R.string.currency_before));
		final String currencyAfter = mPreferenceSettings.getString(getString(R.string.key_currency_after), getString(R.string.currency_after));
		amountText.setText(Payment.formatAmountString(currencyBefore, payment.getAmount(), currencyAfter));

		if (payment.getType() == Payment.TYPE_REMAINDER) {
			typeValue.setVisibility(View.VISIBLE);
			typeText.setVisibility(View.VISIBLE);
			typeValue.setText(payment.getTypeString(getResources()));
		} else {
			typeValue.setVisibility(View.GONE);
			typeText.setVisibility(View.GONE);
		}

		if (payment.getDescription().length() == 0) {
			descriptionText.setVisibility(View.GONE);
			descriptionValue.setVisibility(View.GONE);
		} else {
			descriptionText.setVisibility(View.VISIBLE);
			descriptionValue.setVisibility(View.VISIBLE);
			descriptionValue.setText(payment.getDescription());
		}

		if (payment.getType() == Payment.TYPE_TRANSFER_TO) {
			accountText.setText(R.string.type_transfer_to);
			transferText.setText(R.string.type_transfer_from);
		} else if (payment.getType() == Payment.TYPE_TRANSFER_FROM) {
			accountText.setText(R.string.type_transfer_from);
			transferText.setText(R.string.type_transfer_to);
		} else {
			accountText.setText(R.string.account);
			categoryValue.setText(payment.getSubcategory().getFullName());
		}

		if (payment.getType() == Payment.TYPE_TRANSFER_FROM || payment.getType() == Payment.TYPE_TRANSFER_TO) {
			transferText.setVisibility(View.VISIBLE);
			transferAccountValue.setVisibility(View.VISIBLE);
			categoryText.setVisibility(View.GONE);
			categoryValue.setVisibility(View.GONE);
		} else {
			transferText.setVisibility(View.GONE);
			transferAccountValue.setVisibility(View.GONE);
			categoryText.setVisibility(View.VISIBLE);
			categoryValue.setVisibility(View.VISIBLE);
		}

		if (payment.getAmount() < 0) {
			amountText.setTextColor(resource.getColor(R.color.withdraw));
		} else {
			amountText.setTextColor(resource.getColor(R.color.deposit));
		}

		if (payment.getType() == Payment.TYPE_REMAINDER) {
			remainderDelimiter.setVisibility(View.VISIBLE);
			remainderText.setVisibility(View.VISIBLE);
			remainderText.setText(Payment.formatAmountString(currencyBefore, payment.getRemainder(), currencyAfter));
		} else {
			remainderDelimiter.setVisibility(View.GONE);
			remainderText.setVisibility(View.GONE);
		}

		if (payment.getTransferAccount() != null) {
			transferAccountValue.setText(payment.getTransferAccount().getName());
		}

		if (payment.getRepeating().getType() == Repeating.TYPE_NONE) {
			repeatingTypeValue.setVisibility(View.GONE);
			repeatingTypeText.setVisibility(View.GONE);
			repeatingTypeDate.setVisibility(View.GONE);
		} else {
			repeatingTypeValue.setVisibility(View.VISIBLE);
			repeatingTypeText.setVisibility(View.VISIBLE);
			repeatingTypeDate.setVisibility(View.VISIBLE);
			repeatingTypeValue.setText(payment.getRepeating().getTypeString(getResources()));
			repeatingTypeDate.setText(payment.getRepeating().getOccurrences() + "x " + getString(R.string.to) + " "
			        + payment.getRepeating().getEndDateString(getResources()));
		}

		if (PaymentList.isFirst(position)) {
			previousButton.setEnabled(false);
		} else {
			previousButton.setEnabled(true);
		}

		if (PaymentList.isLast(position)) {
			nextButton.setEnabled(false);
		} else {
			nextButton.setEnabled(true);
		}
	}

	private final OnClickListener cancelListener = new OnClickListener() {
		public void onClick(View v) {
			// Close this Activity
			ShowPayment.this.finish();
		}
	};

	private final OnClickListener previousClickListener = new OnClickListener() {
		public void onClick(View v) {
			position = PaymentList.movePrevious(position);
			payment = PaymentList.getPayment(position);
			payment.getRepeating().computeOccurrences(mDbHelper);
			fillForm();
		}
	};

	private final OnClickListener nextClickListener = new OnClickListener() {
		public void onClick(View v) {
			position = PaymentList.moveNext(position);
			payment = PaymentList.getPayment(position);
			payment.getRepeating().computeOccurrences(mDbHelper);
			fillForm();
		}
	};
}
